using System;
using System.Web.Services;
using System.Xml;

using umlungu.engineX;
/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2007-10-27
	Status:		beta
	Version:	2.0.0
	Build:		20071027
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20071027:	Start of .Net2 version from EmailX.1.0.2
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.emailX {
	/// <summary>
	/// umlungu workflow web services
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class emailX : x_result
	{
		#region Private properties
		private x_email emailx;
		#endregion

		#region Protected methods
		/// <summary>Constructor</summary>
		public emailX() : base("emailX") {
		}
		#endregion

		#region Text email web service methods
		/// <summary>
		/// Sends an plain text email message using the .Net SmtpMail object
		/// </summary>
		/// <param name="To">The destination email address</param>
		/// <param name="From">The sender email address</param>
		/// <param name="Subject">The subject of the email</param>
		/// <param name="Message">The text of the email message</param>
		[WebMethod(Description = "Sends an plain text email message using .Net")] 
		public XmlDocument SendText(string To, string From, string Subject, string Message) {
			try {
				emailx = new x_email();
				emailx.SendText(To, From, Subject, Message);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Tests to see if the email web service is working by sending a message
		/// </summary>
		/// <param name="TestMessage">The body of the test message</param>
		[WebMethod] public XmlDocument TestText(string TestMessage) {
			try {
				emailx = new x_email();
				emailx.SendText(TestMessage);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

		#region Html email web service methods
		/// <summary>
		/// Sends an plain text email message using the .Net SmtpMail object
		/// </summary>
		/// <param name="To">The destination email address</param>
		/// <param name="From">The sender email address</param>
		/// <param name="Subject">The subject of the email</param>
		/// <param name="Message">The text of the email message</param>
		[WebMethod] public XmlDocument SendHtml(string To, string From, string Subject, string Message) {
			try {
				emailx = new x_email();
				emailx.SendHtml(To, From, Subject, Message);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Sends an plain text email message using the .Net SmtpMail object
		/// </summary>
		/// <param name="List">An Xml document that contains a list of nodes containing email addresses</param>
		/// <param name="EmailNode">The name of the elements in the list that have an 'email' attribute</param>
		/// <param name="From">The sender email address</param>
		/// <param name="Subject">The subject of the email</param>
		/// <param name="Message">The text of the email message</param>
		[WebMethod] public XmlDocument SendHtmlList(XmlDocument List, string EmailNode, string From, string Subject, string Message) {
			try {
				emailx = new x_email();
				emailx.SendHtml(List, EmailNode, From, Subject, Message);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Tests to see if the email web service is working by sending a message
		/// </summary>
		/// <param name="TestMessage">The body of the test message</param>
		[WebMethod] public XmlDocument TestHtml(string TestMessage) {
			try {
				string html_header	= "<html><body>";
				string html_footer	= "</body></html>";
				emailx = new x_email();
				emailx.SendHtml(string.Concat(html_header, TestMessage, html_footer));
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

		#region 'Raw' Text email web service methods
		/// <summary>
		/// Sends an plain text email message using direct communication to the SMTP server via sockets
		/// </summary>
		/// <param name="To">The destination email address</param>
		/// <param name="From">The sender email address</param>
		/// <param name="Subject">The subject of the email</param>
		/// <param name="Message">The text of the email message</param>
		[WebMethod] public XmlDocument SendRaw(string To, string From, string Subject, string Message) {
			try {
				emailx = new x_email();
				emailx.SendRaw(To, From, Subject, Message);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Tests the email web service for a list of mails
		/// </summary>
		/// <param name="TestFile">URL for the file containing the XML document containing email nodes.</param>
		/// <param name="ToNodeName">The name of the node with the destination email addressas the attribute "email"</param>
		/// <param name="From">The sender email address</param>
		/// <param name="Subject">The subject of the email</param>
		/// <param name="Message">The text of the email message</param>
		[WebMethod] public XmlDocument SendFile(string TestFile, string ToNodeName, string From, string Subject, string Message) {
			try {
				emailx = new x_email();
				XmlDocument emailList = new XmlDocument();
				emailList.Load(TestFile);
				emailx.SendHtml(emailList, ToNodeName, From, Subject, Message);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Tests to see if the email web service is working by sending a message
		/// </summary>
		/// <param name="TestMessage">The body of the test message</param>
		[WebMethod] public XmlDocument TestRaw(string TestMessage) {
			try {
				emailx = new x_email();
				emailx.SendRaw(TestMessage);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion
	}
}
